package com.seeyon.apps.shangqi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.workflow.event.WorkflowEventData;

import java.util.List;
import java.util.Map;

public class PJshangqi {
    public static String flbxpingjie(Map<Object, Object> jiefang, Map<Object, Object> daifang, List<Map<Object, Object>> minxi) {
        String json = "{\n" +
//                "    \"data\":{\n" +
                "    \"NeedUpDateFields\": [],\n" +
                "    \"NeedReturnFields\": [],\n" +
                "    \"IsDeleteEntry\": \"true\",\n" +
                "    \"SubSystemId\": \"\",\n" +
                "    \"IsVerifyBaseDataField\": \"false\",\n" +
                "    \"IsEntryBatchFill\": \"true\",\n" +
                "    \"ValidateFlag\": \"true\",\n" +
                "    \"NumberSearch\": \"true\",\n" +
                "    \"IsAutoAdjustField\": \"false\",\n" +
                "    \"InterationFlags\": \"\",\n" +
                "    \"IgnoreInterationFlag\": \"\",\n" +
                "    \"IsControlPrecision\": \"false\",\n" +
                "    \n" +
                "    \"ValidateRepeatJson\": \"false\",\n" +
                "    \"Model\": {\n" +
                "        \"FID\": 0,\n" +
                //申请人名称   以前传编码现在改为文本
//                "        \"F_YUKB_ApplicantCode\": {\n" +
//               // "    FSTAFFNUMBER:"  +StrKit.str(jiefang.get("jf_sqrcode")) +
//                "        \"FSTAFFNUMBER\": \""+StrKit.str(jiefang.get("jf_sqrcode"))+"\"\n" +
//                "        }," +
                "        \"F_YUKB_Applicant\": \"" + StrKit.str(jiefang.get("jf_sqrcode")) + "\",\n" +

                //部门名称  以前传编码现在改为文本
//                "        \"F_YUKB_DepartmentCode\": {\n" +
//                "            \"FNUMBER\":  \""+StrKit.str(jiefang.get("jf_depcode"))+"\"\n" +
//                "        },\n" +
                "        \"F_YUKB_Department\": \"" + StrKit.str(jiefang.get("jf_depcode")) + "\",\n" +
                //贷方摘要
                //  "        \"F_YUKB_CreditSummary\": "+StrKit.str(daifang.get("df_dfzy"))+",\n" +
                "        \"F_YUKB_CreditSummary\": \"" + StrKit.str(daifang.get("df_dfzy")) + "\",\n" +
                //组织编码
                "        \"F_YUKB_OrganizationCode\": {\n" +
                // "            \"FNumber\": "+StrKit.str(daifang.get("df_orgcode"))+"\n" +
                "            \"FNumber\": \"" + StrKit.str(daifang.get("df_orgcode")) + "\"\n" +
                "        },\n" +
                //银行科目
                "        \"F_YUKB_bank\": {\n" +
                // "            \"FNUMBER\": "+StrKit.str(daifang.get("df_bank"))+"\n" +
                "            \"FNUMBER\": \"" + StrKit.str(daifang.get("df_bank")) + "\"\n" +
                "        },\n" +
                //付款日期
                //  "        \"F_YUKB_PaymentDate\": "+StrKit.str(daifang.get("df_paydate"))+",\n" +
                "        \"F_YUKB_PaymentDate\": \"" + StrKit.str(daifang.get("df_paydate")) + "\",\n" +
                "        \"F_YUKB_Count\": 0,\n" +
                //贷方本位币  目前不传本位币
                //   "        \"F_YUKB_CreditBaseAmount\":"+StrKit.str(daifang.get("df_money_benwei"))+",\n" +
                //    "        \"F_YUKB_CreditBaseAmount\":\""+StrKit.str(daifang.get("df_money_benwei"))+"\",\n" +
                //贷方原币
                "        \"F_YUKB_CreditOrigiAmount\":" + StrKit.str(daifang.get("df_money")) + ",\n" +
                //汇率
                "        \"F_YUKB_ExchangeRate\":" + StrKit.str(daifang.get("df_hl")) + ",\n" +
                // "        \"F_YUKB_CreditOrigiAmount\":\""+StrKit.str(daifang.get("df_money"))+"\",\n" +
                //OA单号
                //  "        \"F_YUKB_OaDocumentNum\": "+StrKit.str(jiefang.get("jf_orgcode"))+",\n" +
                "        \"F_YUKB_OaDocumentNum\":\"" + StrKit.str(jiefang.get("jf_oacode")) + "\",\n" +
                //原币币别
                "        \"F_YUKB_OriginCURRENCY\": {\n" +
                // "            \"FNUMBER\":"+StrKit.str(daifang.get("df_yb"))+"\n" +
                "            \"FNUMBER\": \"" + StrKit.str(daifang.get("df_yb")) + "\"\n" +
                "        },\n" +
                //本位币币别
                "        \"F_YUKB_StandardCurrency\": {\n" +
                //  "            \"FNUMBER\":"+StrKit.str(jiefang.get("df_bwb"))+"\n" +
                "            \"FNUMBER\": \"" + StrKit.str(daifang.get("df_bwb")) + "\"\n" +
                "        },\n" +
                //summary
                // "        \"F_YUKB_Summary\":"+StrKit.str(jiefang.get("summary"))+",\n" +
                "        \"F_YUKB_Summary\": \"" + StrKit.str(jiefang.get("summary")) + "\",\n" +
                //明细
                "        \"F_YUKB_ExpenseReiEntity\": [\n" +
                ExpenseReiEntity(minxi, "flbx") +
                "        ]\n" +
                "    }\n" +
//                "},\n"+
//                "\"formid\":\"YUKB_ExpenseReimbursement\""+
                "}\n";

        return json;
    }

    //出差申请
    public static String ccsqpingjie(Map<Object, Object> jiefang, Map<Object, Object> daifang, List<Map<Object, Object>> minxi) {
        String json = "{\n" +
                "    \"NeedUpDateFields\": [],\n" +
                "    \"NeedReturnFields\": [],\n" +
                "    \"IsDeleteEntry\": \"true\",\n" +
                "    \"SubSystemId\": \"\",\n" +
                "    \"IsVerifyBaseDataField\": \"false\",\n" +
                "    \"IsEntryBatchFill\": \"true\",\n" +
                "    \"ValidateFlag\": \"true\",\n" +
                "    \"NumberSearch\": \"true\",\n" +
                "    \"IsAutoAdjustField\": \"false\",\n" +
                "    \"InterationFlags\": \"\",\n" +
                "    \"IgnoreInterationFlag\": \"\",\n" +
                "    \"IsControlPrecision\": \"false\",\n" +
                "    \"ValidateRepeatJson\": \"false\",\n" +
                "    \"Model\": {\n" +
                "        \"FID\": 0,\n" +
                //组织机构
                "        \"F_YUKB_OrganizationCode\": {\n" +
                "            \"FNumber\":  \"" + StrKit.str(jiefang.get("jf_orgcode")) + "\",\n" +
                "        },\n" +
                //贷方金额(本位币港币)
                "        \"F_YUKB_CreditAmount\": " + StrKit.str(jiefang.get("jd_money")) + ",\n" +
                //贷方摘要
                "        \"F_YUKB_CreditSummary\": \"" + StrKit.str(jiefang.get("jf_dfzy")) + "\",\n" +
                //付款时间
                "        \"F_YUKB_PaymentDate\":  \"" + StrKit.str(jiefang.get("js_data")) + "\",\n" +
                "        \"F_YUKB_Count\": 0,\n" +
                //oa系统单号
                "        \"F_YUKB_OaDocumentNum\": \"" + StrKit.str(jiefang.get("jf_OAcode")) + "\",\n" +
                //币别
                "        \"F_YUKB_CURRENCY\": {\n" +
                "            \"FNUMBER\":\"" + StrKit.str(jiefang.get("jf_bwb_enumItemCode")) + "\"\n" +
                "        },\n" +
                //summaryid
                "        \"F_YUKB_Summary\": \"" + StrKit.str(jiefang.get("summary")) + "\",\n" +
                //部门
                "        \"F_YUKB_Department\": \"" + StrKit.str(jiefang.get("jf_depcode")) + "\",\n" +
                //申请人
                "        \"F_YUKB_Applicant\": \"" + StrKit.str(jiefang.get("jf_sqrcode")) + "\",\n" +
                //银行科目
                "        \"F_YUKB_Bank\": {\n" +
                "            \"FNUMBER\": \"" + StrKit.str(jiefang.get("jf_bank")) + "\"\n" +
                "        },\n" +
                //明细
                "        \"F_YUKB_BusinessEntity\": [\n" +
                ExpenseReiEntity(minxi, "ccsq") +
                "        ]\n" +
                "    }\n" +
                "}";

        return json;
    }

    //付款申请
    public static String fksqpingjie(Map<Object, Object> jiefang, Map<Object, Object> daifang, List<Map<Object, Object>> minxi) {
        String json = "{\n" +
//                "    \"data\":{\n" +
                "    \"NeedUpDateFields\": [],\n" +
                "    \"NeedReturnFields\": [],\n" +
                "    \"IsDeleteEntry\": \"true\",\n" +
                "    \"SubSystemId\": \"\",\n" +
                "    \"IsVerifyBaseDataField\": \"false\",\n" +
                "    \"IsEntryBatchFill\": \"true\",\n" +
                "    \"ValidateFlag\": \"true\",\n" +
                "    \"NumberSearch\": \"true\",\n" +
                "    \"IsAutoAdjustField\": \"false\",\n" +
                "    \"InterationFlags\": \"\",\n" +
                "    \"IgnoreInterationFlag\": \"\",\n" +
                "    \"IsControlPrecision\": \"false\",\n" +
                "    \n" +
                "    \"ValidateRepeatJson\": \"false\",\n" +
                "    \"Model\": {\n" +
                "        \"FID\": 0,\n" +
                //申请人名称   以前传编码现在改为文本
//                "        \"F_YUKB_ApplicantCode\": {\n" +
//               // "    FSTAFFNUMBER:"  +StrKit.str(jiefang.get("jf_sqrcode")) +
//                "        \"FSTAFFNUMBER\": \""+StrKit.str(jiefang.get("jf_sqrcode"))+"\"\n" +
//                "        }," +
                "        \"F_YUKB_Applicant\": \"" + StrKit.str(jiefang.get("jf_sqrcode")) + "\",\n" +

                //部门名称  以前传编码现在改为文本
//                "        \"F_YUKB_DepartmentCode\": {\n" +
//                "            \"FNUMBER\":  \""+StrKit.str(jiefang.get("jf_depcode"))+"\"\n" +
//                "        },\n" +
                "        \"F_YUKB_Department\": \"" + StrKit.str(jiefang.get("jf_depcode")) + "\",\n" +
                //贷方摘要
                //  "        \"F_YUKB_CreditSummary\": "+StrKit.str(daifang.get("df_dfzy"))+",\n" +
                "        \"F_YUKB_CreditSummary\": \"" + StrKit.str(daifang.get("df_dfzy")) + "\",\n" +
                //组织编码
                "        \"F_YUKB_OrganizationCode\": {\n" +
                // "            \"FNumber\": "+StrKit.str(daifang.get("df_orgcode"))+"\n" +
                "            \"FNumber\": \"" + StrKit.str(daifang.get("df_orgcode")) + "\"\n" +
                "        },\n" +
                //银行科目
                "        \"F_YUKB_bank\": {\n" +
                // "            \"FNUMBER\": "+StrKit.str(daifang.get("df_bank"))+"\n" +
                "            \"FNUMBER\": \"" + StrKit.str(jiefang.get("jf_bank")) + "\"\n" +
                "        },\n" +
                //付款日期
                //  "        \"F_YUKB_PaymentDate\": "+StrKit.str(daifang.get("df_paydate"))+",\n" +
                "        \"F_YUKB_PaymentDate\": \"" + StrKit.str(jiefang.get("jf_date")) + "\",\n" +

                "        \"F_YUKB_Count\": 0,\n" +
                //贷方本位币  目前不传本位币
                //   "        \"F_YUKB_CreditBaseAmount\":"+StrKit.str(daifang.get("df_money_benwei"))+",\n" +
                //  "        \"F_YUKB_CreditBaseAmount\":\""+StrKit.str(daifang.get("df_money_benwei"))+"\",\n" +
                //原币
                "        \"F_YUKB_CreditOrigiAmount\":" + StrKit.str(jiefang.get("jf_money")) + ",\n" +
                //汇率
                "        \"F_YUKB_ExchangeRate\":" + StrKit.str(daifang.get("df_hl")) + ",\n" +
                // "        \"F_YUKB_CreditOrigiAmount\":\""+StrKit.str(daifang.get("df_money"))+"\",\n" +
                //OA单号
                //  "        \"F_YUKB_OaDocumentNum\": "+StrKit.str(jiefang.get("jf_orgcode"))+",\n" +
                "        \"F_YUKB_OaDocumentNum\":\"" + StrKit.str(jiefang.get("jf_orgcode")) + "\",\n" +
                //原币币别
                "        \"F_YUKB_OriginCURRENCY\": {\n" +
                // "            \"FNUMBER\":"+StrKit.str(daifang.get("df_yb"))+"\n" +
                "            \"FNUMBER\": \"" + StrKit.str(jiefang.get("jf_yb_enumItemCode")) + "\"\n" +
                "        },\n" +
                //本位币币别
                "        \"F_YUKB_StandardCurrency\": {\n" +
                //  "            \"FNUMBER\":"+StrKit.str(jiefang.get("df_bwb"))+"\n" +
                "            \"FNUMBER\": \"" + StrKit.str(daifang.get("df_bwb_enumItemCode")) + "\"\n" +
                "        },\n" +
                //summary
                // "        \"F_YUKB_Summary\":"+StrKit.str(jiefang.get("summary"))+",\n" +
                "        \"F_YUKB_Summary\": \"" + StrKit.str(jiefang.get("summary")) + "\",\n" +
                //明细
                "        \"F_YUKB_PaymentEntity\": [\n" +
                ExpenseReiEntity(minxi, "fksq") +
                "        ]\n" +
                "    }\n" +
//                "},\n"+
//                "\"formid\":\"YUKB_ExpenseReimbursement\""+
                "}\n";

        return json;
    }


    public static String ExpenseReiEntity(List<Map<Object, Object>> minxi, String code) {
        StringBuilder stringBuilder = new StringBuilder();
        if (code.equals("flbx")) {
            for (int i = 0; i < minxi.size(); i++) {
                stringBuilder.append("            {\n" +
                        //借方摘要
                        // "                \"F_YUKB_DebitSummary\": "+StrKit.str(minxi.get(i).get("minxi_jfzy"))+",\n" +
                        "                \"F_YUKB_DebitSummary\":\"" + StrKit.str(minxi.get(i).get("minxi_jfzy")) + "\",\n" +
                        "                \"F_YUKB_SubjectCode\": {\n" +
                        //科目编码
                        //  "                    \"FNUMBER\": "+StrKit.str(minxi.get(i).get("minxi_name"))+",\n" +
                        "                    \"FNUMBER\":\"" + StrKit.str(minxi.get(i).get("minxi_code")) + "\"\n" +
                        "                },\n" +
                        //借方本位币  需要改不是moeny参数
                        // "                \"F_YUKB_DebitBaseAmount\":"+StrKit.str(minxi.get(i).get("minxi_moeny"))+",\n" +
                        //借方原币
                        "                \"F_YUKB_DebitOrigiAmount\": " + StrKit.str(minxi.get(i).get("minxi_moenygb")) + "\n" +
                        "            },");

            }
        } else if (code.equals("ccsq")) {
            for (int i = 0; i < minxi.size(); i++) {
                stringBuilder.append("            {\n" +
                        //借方摘要
                        // "                \"F_YUKB_DebitSummary\": "+StrKit.str(minxi.get(i).get("minxi_jfzy"))+",\n" +
                        "                \"F_YUKB_DebitSummary\":\"" + StrKit.str(minxi.get(i).get("minxi_jfzy")) + "\",\n" +
                        "                \"F_YUKB_SubjectCode\": {\n" +
                        //科目编码
                        //  "                    \"FNUMBER\": "+StrKit.str(minxi.get(i).get("minxi_name"))+",\n" +
                        "                    \"FNUMBER\":\"" + StrKit.str(minxi.get(i).get("minxi_code")) + "\"\n" +
                        "                },\n" +
                        //借方本位币  需要改不是moeny参数
                        // "                \"F_YUKB_DebitBaseAmount\":"+StrKit.str(minxi.get(i).get("minxi_moeny"))+",\n" +
                        //借方原币
                        "                \"F_YUKB_DebitAmount\": " + StrKit.str(minxi.get(i).get("minxi_moeny")) + "\n" +
                        "            },");

            }
        } else if (code.equals("fksq")) {
            for (int i = 0; i < minxi.size(); i++) {
                stringBuilder.append("            {\n" +
                        //借方摘要
                        // "                \"F_YUKB_DebitSummary\": "+StrKit.str(minxi.get(i).get("minxi_jfzy"))+",\n" +
                        "                \"F_YUKB_DebitSummary\":\"" + StrKit.str(minxi.get(i).get("minxi_jfzy")) + "\",\n" +
                        "                \"F_YUKB_SubjectCode\": {\n" +
                        //科目编码
                        //  "                    \"FNUMBER\": "+StrKit.str(minxi.get(i).get("minxi_name"))+",\n" +
                        "                    \"FNUMBER\":\"" + StrKit.str(minxi.get(i).get("minxi_code")) + "\"\n" +
                        "                },\n" +
                        //借方本位币  需要改不是moeny参数
                        // "                \"F_YUKB_DebitBaseAmount\":"+StrKit.str(minxi.get(i).get("minxi_moeny"))+",\n" +
                        //借方原币
                        "                \"F_YUKB_DebitOrigiAmount\": " + StrKit.str(minxi.get(i).get("minxi_moeny")) + "\n" +
                        "            },");

            }
        }


        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);


        return substring;
    }

    public static String getcode(String input) {
        StringBuffer result = new StringBuffer();
        String code = "";
        // 添加前4个字符
        result.append(input, 0, 4);
        result.append("."); // 添加分隔符
        // 添加后续字符，每2个字符分割
        for (int i = 4; i < input.length(); i += 2) {
            // 计算当前分割的结束位置
            int end = Math.min(i + 2, input.length());
            result.append(input, i, end);
            // 添加分隔符，除非是最后一段
            if (end < input.length()) {
                result.append(".");
            }
        }
//        // 遍历字符串，每次取 length 个字符
//        for (int o = 0; o < code.length(); o += 4) {
//            // 计算当前分割的结束位置
//            int end = Math.min(o +4, code.length());
//            // 获取子字符串并添加到结果中
//            code1.append(code, o, end);
//            // 可以在这里添加分隔符，例如空格或其他字符
//            if (end < code.length()) {
//                code1.append("."); // 添加空格作为分隔符
//            }
//        }

        code = result.toString();

        return code;
    }

}

